package com.lb.activity.rest;

import com.lb.activity.po.ExamineFlowStep;
import com.lb.activity.service.LeaveBillService;
import com.lb.activity.service.PurchaseApplyService;
import com.lb.activity.util.ResultUtil;
import com.lb.activity.vo.PurchaseApplyVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RestController
@RequestMapping("purchase")
@Api(value = "采购申请")
public class PurchaseController {

    @Autowired
    private PurchaseApplyService purchaseApplyService;

    @Autowired
    private LeaveBillService leaveBillService;

    @GetMapping("purchase")
    @ApiOperation(value = "跳转采购申请页面")
    public ModelAndView qingjia(){
        return new ModelAndView("person/purchase/purchase");
    }

    @GetMapping("toAdd")
    @ApiOperation(value = "到添加页面")
    public ModelAndView toAdd(){
        return new ModelAndView("person/purchase/purchaseAdd");
    }

    @PostMapping("add")
    @ApiOperation(value = "添加采购记录,开始流程")
    public ResultUtil add(PurchaseApplyVo purchaseApplyVo){
        ResultUtil resultUtil = purchaseApplyService.add(purchaseApplyVo);
        return resultUtil;
    }

    @GetMapping("myPurchase")
    @ApiOperation(value = "我的运行中采购申请集合")
    public ResultUtil myPurchase(PurchaseApplyVo purchaseApplyVo){
        ResultUtil resultUtil = purchaseApplyService.myPurchase(purchaseApplyVo);
        return resultUtil;
    }

    @GetMapping("myPurchaseAgain")
    @ApiOperation(value = "我的待重新提交采购申请集合")
    public ResultUtil myPurchaseAgin(PurchaseApplyVo purchaseApplyVo){
        ResultUtil resultUtil = purchaseApplyService.myPurchaseAgain(purchaseApplyVo);
        return resultUtil;
    }

    @GetMapping("toPurchaseAgain")
    @ApiOperation(value = "我的待重新提交采购申请页面")
    public ModelAndView toPurchaseAgain(String taskId){
        ModelAndView modelAndView = new ModelAndView("person/purchase/purchaseAgain");
        ResultUtil resultUtil = purchaseApplyService.procurementDetail(taskId);
        List<ExamineFlowStep> stepList = leaveBillService.getExamineStep(((PurchaseApplyVo)resultUtil.getData()).getId());
        modelAndView.addObject("purchase",resultUtil.getData());
        modelAndView.addObject("stepList",stepList);
        modelAndView.addObject("taskId",taskId);
        return modelAndView;
    }

    @PostMapping("purchaseAgain")
    @ApiOperation(value = "重新提交采购申请")
    public ResultUtil purchaseAgain(String taskId,String approve){
        ResultUtil resultUtil = purchaseApplyService.purchaseAgain(taskId,approve);
        return resultUtil;
    }


    @GetMapping("pendingReceipt")
    @ApiOperation(value = "待收货流程")
    public ResultUtil pendingReceipt(PurchaseApplyVo purchaseApplyVo){
        ResultUtil resultUtil = purchaseApplyService.pendingReceipt(purchaseApplyVo);
        return resultUtil;
    }

    @GetMapping("toTakeGoods")
    @ApiOperation(value = "跳转到收货流程页面")
    public ModelAndView toTakeGoods(String taskId){
        ModelAndView modelAndView = new ModelAndView("person/purchase/takeGood");
        ResultUtil resultUtil = purchaseApplyService.procurementDetail(taskId);
        List<ExamineFlowStep> stepList = leaveBillService.getExamineStep(((PurchaseApplyVo)resultUtil.getData()).getId());
        modelAndView.addObject("purchase",resultUtil.getData());
        modelAndView.addObject("stepList",stepList);
        modelAndView.addObject("taskId",taskId);
        return modelAndView;
    }

    @PostMapping("takeGood")
    @ApiOperation(value = "收货")
    public ResultUtil takeGood(String taskId){
        ResultUtil resultUtil = purchaseApplyService.takeGood(taskId);
        return resultUtil;
    }

    @GetMapping("myPurchaseAll")
    @ApiOperation(value = "我的所有采购申请")
    public ResultUtil myPurchaseAll(PurchaseApplyVo purchaseApplyVo){
        ResultUtil resultUtil = purchaseApplyService.myPurchaseAll(purchaseApplyVo);
        return resultUtil;
    }

    @GetMapping("toViewPurchase")
    @ApiOperation(value = "跳转我的采购申请页面")
    public ModelAndView toViewPurchase(Long id){
        ModelAndView modelAndView = new ModelAndView("person/purchase/purchaseDetail");
        ResultUtil resultUtil = purchaseApplyService.purchaseDetail(id);
        List<ExamineFlowStep> stepList = leaveBillService.getExamineStep(id);
        modelAndView.addObject("purchase",resultUtil.getData());
        modelAndView.addObject("stepList",stepList);
        return modelAndView;
    }
    //*******************************采购经理开始*****************************************

    @GetMapping("procurement")
    @ApiOperation(value = "采购经理审批页面")
    @RequiresPermissions(value = "procurement")
    public ModelAndView procurement(){
       return new ModelAndView("person/purchase/procurementManager");
    }

    @GetMapping("procurements")
    @ApiOperation(value = "审批数据集合")
    @RequiresPermissions(value = {"procurement","finance","chuna","generalManager"},logical = Logical.OR)
    public ResultUtil procurements(PurchaseApplyVo purchaseApplyVo){
        ResultUtil resultUtil = purchaseApplyService.procurement(purchaseApplyVo);
        return resultUtil;
    }

    @GetMapping("toProcurementDeal")
    @ApiOperation(value = "采购经理审批跳转到详情页面")
    @RequiresPermissions(value = "procurement")
    public ModelAndView toProcurementDeal(String taskId){
        ModelAndView modelAndView = new ModelAndView("person/purchase/procurementManagerDeal");
        ResultUtil resultUtil = purchaseApplyService.procurementDetail(taskId);
        List<ExamineFlowStep> stepList = leaveBillService.getExamineStep(((PurchaseApplyVo)resultUtil.getData()).getId());
        modelAndView.addObject("purchase",resultUtil.getData());
        modelAndView.addObject("stepList",stepList);
        modelAndView.addObject("taskId",taskId);
        return modelAndView;
    }

    @PostMapping("procurementDeal")
    @ApiOperation(value = "采购经理审批")
    @RequiresPermissions(value = "procurement")
    public ResultUtil procurementDeal(String taskId,String approve){
        ResultUtil resultUtil = purchaseApplyService.procurementDeal(taskId,approve);
        return resultUtil;
    }

    //*******************************采购经理结束*****************************************


    //*******************************财务审批开始*****************************************

    @GetMapping("finance")
    @ApiOperation(value = "财务审批页面")
    @RequiresPermissions(value = "finance")
    public ModelAndView finance(){
        return new ModelAndView("person/purchase/financeManager");
    }

    @GetMapping("toFinanceDeal")
    @ApiOperation(value = "财务批跳转到详情页面")
    @RequiresPermissions(value = "finance")
    public ModelAndView toFinanceDeal(String taskId){
        ModelAndView modelAndView = new ModelAndView("person/purchase/financeManagerDeal");
        ResultUtil resultUtil = purchaseApplyService.procurementDetail(taskId);
        List<ExamineFlowStep> stepList = leaveBillService.getExamineStep(((PurchaseApplyVo)resultUtil.getData()).getId());
        modelAndView.addObject("purchase",resultUtil.getData());
        modelAndView.addObject("stepList",stepList);
        modelAndView.addObject("taskId",taskId);
        return modelAndView;
    }

    @PostMapping("financeDeal")
    @ApiOperation(value = "财务审批")
    @RequiresPermissions(value = "finance")
    public ResultUtil financeDeal(String taskId,String approve){
        ResultUtil resultUtil = purchaseApplyService.financeDeal(taskId,approve);
        return resultUtil;
    }

    //*******************************财务审批结束*****************************************

    //*******************************出纳审批开始*****************************************

    @GetMapping("chuna")
    @ApiOperation(value = "出纳页面")
    @RequiresPermissions(value = "chuna")
    public ModelAndView chuna(){
        return new ModelAndView("person/purchase/chuna");
    }

    @GetMapping("toChunaDeal")
    @ApiOperation(value = "出纳审批跳转到详情页面")
    @RequiresPermissions(value = "chuna")
    public ModelAndView toChunaDeal(String taskId){
        ModelAndView modelAndView = new ModelAndView("person/purchase/chunaDeal");
        ResultUtil resultUtil = purchaseApplyService.procurementDetail(taskId);
        List<ExamineFlowStep> stepList = leaveBillService.getExamineStep(((PurchaseApplyVo)resultUtil.getData()).getId());
        modelAndView.addObject("purchase",resultUtil.getData());
        modelAndView.addObject("stepList",stepList);
        modelAndView.addObject("taskId",taskId);
        return modelAndView;
    }

    @PostMapping("chunaDeal")
    @ApiOperation(value = "出纳审批")
    @RequiresPermissions(value = "chuna")
    public ResultUtil chunaDeal(String taskId){
        ResultUtil resultUtil = purchaseApplyService.chunaDeal(taskId);
        return resultUtil;
    }

    //*******************************出纳审批结束*****************************************

    //*******************************总经理审批开始****************************************
    @GetMapping("generalManager")
    @ApiOperation(value = "总经理页面")
    @RequiresPermissions(value = "generalManager")
    public ModelAndView generalManager(){
        return new ModelAndView("person/purchase/generalManager");
    }

    @GetMapping("toGeneralManagerDeal")
    @ApiOperation(value = "总经理审批跳转到详情页面")
    @RequiresPermissions(value = "generalManager")
    public ModelAndView toGeneralManagerDeal(String taskId){
        ModelAndView modelAndView = new ModelAndView("person/purchase/generalManagerDeal");
        ResultUtil resultUtil = purchaseApplyService.procurementDetail(taskId);
        List<ExamineFlowStep> stepList = leaveBillService.getExamineStep(((PurchaseApplyVo)resultUtil.getData()).getId());
        modelAndView.addObject("purchase",resultUtil.getData());
        modelAndView.addObject("stepList",stepList);
        modelAndView.addObject("taskId",taskId);
        return modelAndView;
    }

    @PostMapping("generalManagerDeal")
    @ApiOperation(value = "总经理审批")
    @RequiresPermissions(value = "generalManager")
    public ResultUtil generalManagerDeal(String taskId,String approve){
        ResultUtil resultUtil = purchaseApplyService.generalManagerDeal(taskId,approve);
        return resultUtil;
    }

    //*******************************总经理审批结束****************************************


}
